package com.yanqu.road.server.cmd.activity.seacraft.pirateriot;

import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotUserData;
import com.yanqu.road.pb.activity.SeacraftPirateRiotProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.seacraft.SeacraftPirateRiotModule;
import com.yanqu.road.server.pb.SeacraftPirateRiotPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SEACRAFT_PIRATE_RIOT_START_CHAPTER, desc = "开始新关卡")
public class SeacraftPirateRiotStartChapterCmd extends AbstractCommand {

    private int chapterId;
    private int skip = -1;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SeacraftPirateRiotProto.SeacraftPirateRiotStartChapterReqMsg reqMsg = SeacraftPirateRiotProto.SeacraftPirateRiotStartChapterReqMsg.parseFrom(packet.getBytes());
        chapterId = reqMsg.getChapterId();
        if (reqMsg.hasSkip()) {
            skip = reqMsg.getSkip() ? 1 : 0;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = player.getModule(SeacraftPirateRiotModule.class).startChapter(chapterId, skip);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            SeacraftPirateRiotProto.SeacraftPirateRiotStartChapterRespMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotStartChapterRespMsg.newBuilder();
            builder.setRet(ret);
            SeacraftPirateRiotUserData userData = player.getModule(SeacraftPirateRiotModule.class).getUserData();
            if (userData != null) {
                builder.setUserData(SeacraftPirateRiotPb.parseSeacraftPirateRiotUserDataMsg(player, userData));
                builder.setChapterData(SeacraftPirateRiotPb.parseSeacraftPirateRiotChapterMsg(userData));
            }
            player.sendPacket(ClientProtocol.U_SEACRAFT_PIRATE_RIOT_START_CHAPTER, builder);
        }
    }
}
